package com.springcloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.entities.CommonResult;
import com.springcloud.entities.Payment;
import com.springcloudalibaba.myhandler.CustomerBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * Title: FlowLimitController
 * Description: TODO
 *
 * @author gejx
 * @version V1.0
 * @date 2022-01-10
 */
@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA()
    {
        try {
            TimeUnit.MILLISECONDS.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {log.info(Thread.currentThread().getName()+"...........testB");
        return "------testB";
    }
    @GetMapping("/testD")
    public String testD()
    {
        //暂停几秒钟线程
//        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        log.info("testD 测试RT");
//        int a=10/0;
        return "------testD";
    }
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_HotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        return "testHotKey";
    }
    public String deal_HotKey(String p1, String p2, BlockException exception){
    return "deal_HotKeyo(╥﹏╥)o";
    }

    /**
     * Title: RateLimitController
     * Description: TODO
     *
     * @author gejx
     * @version V1.0
     * @date 2022-01-11
     */
    @RestController
    public static class RateLimitController {
        @GetMapping("/byResource")
        @SentinelResource(value = "byResource",blockHandler = "handleException")
        public CommonResult byResource()
        {
            return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
        }
        public CommonResult handleException(BlockException exception)
        {
            return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
        }

        @GetMapping("/rateLimit/byUrl")
        @SentinelResource(value = "byUrl")
        public CommonResult byUrl()
        {
            return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
        }

        /**
         * 自定义通用的限流处理逻辑，
         blockHandlerClass = CustomerBlockHandler.class
         blockHandler = handleException2
         上述配置：找CustomerBlockHandler类里的handleException2方法进行兜底处理
         */
        /**
         * 自定义通用的限流处理逻辑
         */
        @GetMapping("/rateLimit/customerBlockHandler")
        @SentinelResource(value = "customerBlockHandler",
                blockHandlerClass = CustomerBlockHandler.class, //处理异常的类
                blockHandler = "handleException2")
        public CommonResult customerBlockHandler()
        {
            return new CommonResult(200,"按客户自定义限流处理逻辑");
        }

    }
}
